package huawei.js2025;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * 积木最远距离
 */
public class Question14 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        int[] ints = new int[n];
        for (int i = 0; i < n; i++) {
            ints[i] = sc.nextInt();
        }

        Map<Integer, Integer> map = new HashMap<>(); // 记录积木上数字第一次出现的位置
        int maxLen = -1;
        for (int i = 0; i < n; i++) {
            if (map.containsKey(ints[i])) {
                int len = i - map.get(ints[i]);
                maxLen = Math.max(maxLen, len);
            } else {
                map.put(ints[i], i);
            }
        }

        System.out.println(maxLen);
    }
}
